package dat255.calendarclock.controller.event;

import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import dat255.calendarclock.R;
import dat255.calendarclock.model.event.EventHandler;
import dat255.calendarclock.model.event.SubEvent;
import dat255.calendarclock.model.event.interval.Interval;
import dat255.calendarclock.model.event.interval.MonthlyInterval;
import dat255.calendarclock.model.event.interval.OneTimeInterval;
import dat255.calendarclock.model.event.interval.WeeklyInterval;

/**
 * A controller that provides the Contentview width the correct
 * Event-properties.
 * 
 * @author Marcus Hultman, Gabriel Andersson & Oskar Montin
 * 
 */
public class ShowEventController extends Activity {
	//Shown event
	private static SubEvent subEvent;

	//Fields
	private static TextView nameTextField;
	private static TextView DescTextField;
	private static TextView categoryTextField;
	private static TextView reccuranceTextField;
	private static TextView timeTextField;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.showevent_layout);
		if (EventHandler.getInstance().getSelectedEvent() == null) {
			ShowEventController.this.finish();
		}
		// The view has been summoned, so load the data from the selected event
		updateTextFields();
	}

	@Override
	protected void onResume() {

		super.onResume();
		if (EventHandler.getInstance().getSelectedEvent() == null) {
			ShowEventController.this.finish();
		} else {
			updateTextFields();
		}
	}
	/**
	 * Update fields to match the event
	 */
	private void updateTextFields() {
		subEvent = EventHandler.getInstance().getSelectedEvent();
		EventHandler.getInstance().setSelectedDay(subEvent.getDate());
		nameTextField = (TextView) findViewById(R.id.showEvent_name);
		nameTextField.setText(subEvent.getEvent().getName());
		DescTextField = (TextView) findViewById(R.id.showEvent_description);
		DescTextField.setText(subEvent.getEvent().getDescription());
		categoryTextField = (TextView) findViewById(R.id.showEvent_category);
		categoryTextField.setText(subEvent.getEvent().getCategory().getName());
		reccuranceTextField = (TextView) findViewById(R.id.showEvent_reccurance);
		Interval i = subEvent.getEvent().getInterval();
		if(i instanceof OneTimeInterval)
			reccuranceTextField.setText("Once");
		if(i instanceof WeeklyInterval){
			reccuranceTextField.setText("weekly");
			}
		if(i instanceof MonthlyInterval){
			reccuranceTextField.setText("Monthly");
		}
		String date = new SimpleDateFormat("HH:mm - E d MM, yyyy").format(subEvent.getEvent());
		timeTextField = (TextView) findViewById(R.id.showEvent_date);
		timeTextField.setText(date);
	}

	@Override
	protected void onStop() {
		super.onStop();
		EventHandler.getInstance().setSelectedEvent(null);
		this.finish();
	}

	/**
	 * Edit event activity is started
	 */
	public void onEditButtonClick(View view) {
		startActivity(new Intent(ShowEventController.this,
				EditEventController.class));
	}
	
}